package com.second.service.impl;

import com.second.mapper.FReplyMapper;
import com.second.mapper.FUserMapper;
import com.second.model.dto.reply.FReplyDto;
import com.second.model.dto.reviews.FUserCommentDto;
import com.second.model.pojo.FComment;
import com.second.model.pojo.FCommentPicture;
import com.second.model.pojo.FReply;
import com.second.model.pojo.FUser;
import com.second.service.IFReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: MuTing
 * @date: 2022-04-17 20:37
 */
@Service
@Transactional
public class FReplyServiceImpl implements IFReplyService {

    @Autowired
    private FUserMapper fUserMapper;

    @Autowired
    private FReplyMapper fReplyMapper;

    @Override
    public List<FReplyDto> findByCommentIdAndReplyId(Integer commentId,Integer replyId) {
        List <FReply> fReplyList = fReplyMapper.findByCommentIdAndReplyId(commentId,replyId);

        List<FReplyDto> fRList = new ArrayList<>();

        for (FReply fReply : fReplyList) {
            FUser user = fUserMapper.getById(fReply.getUserId());
            FReplyDto fReplyDto = new FReplyDto();
            fReplyDto.setCommentId(fReply.getCommentId());
            fReplyDto.setId(fReply.getId());
            fReplyDto.setUserName(user.getUsername());
            fReplyDto.setContent(fReply.getContent());
            fReplyDto.setReplyTime(fReply.getReplyTime());
            fRList.add(fReplyDto);
        }
        return fRList;
    }

    @Override
    public void addReply(FReply fReply) {
        fReply.setReplyTime(new Date());
        if (null==fReply.getReplyId()){
            fReply.setReplyId(0);
        }
        fReplyMapper.save(fReply);
    }

    @Override
    public void updateReply(FReply fReply) {
        fReplyMapper.save(fReply);
    }

    @Override
    public void delByReplyId(Integer id) {
        fReplyMapper.delByReplyId(id);
    }
}
